papermanagermodule.controller('errorQuestionModal', ['$scope', 'errorQuestionFactory', '$rootScope', 'sysUserInfo', 'RequestTrainService', 'Comm', 'MsgCon', 'Msg', '$sce', '$stateParams', '$state', function ($scope, errorQuestionFactory, $rootScope, sysUserInfo, RTS, Comm, MsgCon, Msg, $sce, $stateParams, $state) {

    var timer;
    //关闭窗口
    $scope.closeWindow = function () {
        clearTimeout(timer);
        errorQuestionFactory.deactivate();
    }
    $scope.isloading1 = false;
    //初始化参数
    $scope.params = {
        paperid: $scope.resultid,
        orgid: sysUserInfo.getuserinfo().organization_ID,
        userid: sysUserInfo.getuserinfo().user_ID,
        username: sysUserInfo.getuserinfo().user_Name,
        state: "1"//1查看，2导出
    };

    $scope.staticObj = {
        creatUsername: "",
        creatDate: ""
    };

    //查询最后统计时间，没有获取到数据，需要请求统计
    $scope.GetTotleInfo = function () {
        $scope.isloading1 = true;
        RTS.findErrorExamList($scope.params).then(function (data) {
            $scope.isloading1 = false;
            if (data.errorcode == "0") {
                $scope.staticObj = data.data;
                if ($scope.staticObj == undefined || $scope.staticObj.creatUsername == undefined || $scope.staticObj.creatUsername.length <= 0) {
                    //开始请求统计
                    $scope.staticErrorQuestion();
                } else {
                    $scope.GetErrorList();
                }
            } else {
                Msg.error("请求数据有误");
            }
        }, function callback() {
            Msg.error("请求数据有误");
            $scope.isloading1 = false;
        });
    }

    $scope.errorList = [];
    //查询错题统计
    $scope.GetErrorList = function () {
        $scope.isloading1 = true;
        RTS.findErrorExam($scope.params).then(function (data) {
            $scope.isloading1 = false;
            if (data.errorcode == "0") {
                $scope.errorList = data.datas;
            } else {
                Msg.error("请求数据有误");
            }
        }, function callback() {
            Msg.error("请求数据有误");
            $scope.isloading1 = false;
        });
    }

    //重新统计
    $scope.staticErrorQuestion = function () {
        //$scope.isloading1 = true;
        RTS.errorExam($scope.params).then(function (data) {
            //$scope.isloading1 = false;
            if (data.errorcode == "0") {
                //统计成功，请求查询
                $scope.GetErrorList();
            } else if (data.errorcode == "73") {
                setTimeout(function () {
                    $scope.staticErrorQuestion();
                }, 10000);
                //$scope.isloading1 = true;
                Msg.warning("正在统计请稍后..");
            } else {
                Msg.error("请求数据有误");
            }
        }, function callback() {
            Msg.error("请求数据有误");
            //$scope.isloading1 = false;
        });
    }
    //导出数据
    $scope.exportData = function () {
        $scope.isloading1 = true;
        RTS.findErrorExam({ paperid: $scope.resultid, state: 2 }).then(function (data) {
            $scope.isloading1 = false;
            if (data.errorcode == "0") {
                window.location = data.errormsg;
            } else if (data.errorcode == "73") {
                timer = setTimeout(function () {
                    $scope.exportData();
                }, 5000);
                $scope.isloading1 = true;
                Msg.warning("正在统计请稍后..");
            } else {
                Msg.error("请求数据有误");
            }
        }, function callback() {
            Msg.error("请求数据有误");
            $scope.isloading1 = false;
        });
    }
    $scope.GetTotleInfo();
} ]);
